package com.eastjet.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eastjet.equipment.mapper.DeviceInspectionprojectMapper;
import com.eastjet.equipment.mapper.DeviceInspectiontaskResultMapper;
import com.eastjet.equipment.mapper.SelectTypeMapper;
import com.eastjet.equipment.service.DeviceInspectionprojectService;
import com.eastjet.equipment.service.DeviceRulesService;
import com.eastjet.equipment.util.CodeRules;
import com.eastjet.equipment.util.PageUtil;
import com.eastjet.equipment.util.RedisUtil;
import com.eastjet.maintenance.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
* @author 杨博文
* @description 针对表【device_inspectionproject(巡检项目表)】的数据库操作Service实现
* @createDate 2024-11-25 09:23:38
*/
@Service
public  class DeviceInspectionprojectServiceImpl extends ServiceImpl<DeviceInspectionprojectMapper, DeviceInspectionproject>
    implements DeviceInspectionprojectService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private DeviceInspectionprojectMapper deviceInspectionprojectMapper;
    @Autowired
    private SelectTypeMapper selectTypeMapper;
    @Autowired
    private DeviceInspectiontaskResultMapper deviceInspectiontaskResultMapper;

    @Autowired
    private DeviceRulesService deviceRulesService;

    @Autowired
    private CodeRules codeRules;

    @Override
    public Page<DeviceInspectionproject> getProjectList(int page, int size, String condition) {
        // 定义Redis中的键
        String redisKey = "ProjectList:";
        System.out.println("ProjectList-condition:"+condition);
        // 先查缓存Redis
        if (redisUtil.hasKey(redisKey)) {
            System.out.println(1);
            System.out.println("ProjectList-从Redis中获取数据");
            List<DeviceInspectionproject> projectList;
            if (condition != null  && !condition.isEmpty() && condition !="") {
                System.out.println(2);
                projectList = deviceInspectionprojectMapper.select(condition); // 获取所有数据
            } else {
                System.out.println(3);
                projectList   = (List<DeviceInspectionproject>) redisUtil.get(redisKey);
            }
            // 使用PageUtil进行分页
            return PageUtil.paginate(projectList, page, size);
        } else {
            System.out.println(4);
            System.out.println("在数据库中查询projectList");
            List<DeviceInspectionproject> projectList;
            // 根据condition条件查询
            if (condition != null && !condition.isEmpty() && condition !="") {
                System.out.println(5);
                projectList = deviceInspectionprojectMapper.select(condition); // 获取所有数据
            } else {
                System.out.println(6);
                condition="";
                projectList = deviceInspectionprojectMapper.select(condition); // 获取所有数据
                // 将数据存入Redis
                redisUtil.set(redisKey, projectList);
            }
            // 使用PageUtil进行分页
            return PageUtil.paginate(projectList, page, size);
        }
    }

    @Override
    public int updateState(int id, int status) {
        String redisKey = "ProjectList:";
        int update = 0;
        if(redisUtil.hasKey(redisKey)){
            redisUtil.del(redisKey);
            UpdateWrapper uw=new UpdateWrapper();
            uw.eq("InspectionProject_id",id);
            uw.set("InspectionProject_state",status);
             update = deviceInspectionprojectMapper.update(new DeviceInspectionproject(), uw);
        }
        return update;
    }

    @Override
    public int del(int id, int isDelete) {
        String redisKey = "ProjectList:";
        int del = 0;
        if(redisUtil.hasKey(redisKey)){
            redisUtil.del(redisKey);
            UpdateWrapper uw=new UpdateWrapper();
            uw.eq("InspectionProject_id",id);
            uw.set("is_delete",isDelete);
            del = deviceInspectionprojectMapper.update(new DeviceInspectionproject(), uw);
        }
        return del;
    }

    @Override
    public String projectAdd(DeviceInspectionproject deviceInspectionproject) {
        System.out.println("project:" + deviceInspectionproject.toString());
        try {
            System.out.println("更新设备维修经验库");
            // 尝试将数据插入数据库


            if (deviceInspectionprojectMapper.insert(deviceInspectionproject) > 0) {
                // 定义Redis的键
                String redisKey = "ProjectList:";
                try {
                    // 从数据库获取当前所有数据
                    String condition="";
                    List<DeviceInspectionproject> deviceInspectionprojectList = deviceInspectionprojectMapper.select(null); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, deviceInspectionprojectList);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "添加成功";
            } else {
                return "添加失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "添加失败";
        }

    }

    @Override
    public String SelectAdd(SelectType selectType) {
        System.out.println("selectType:" + selectType.toString());
        try {
            System.out.println("更新设备维修经验库");
            // 尝试将数据插入数据库


            if (selectTypeMapper.insert(selectType) > 0) {
                // 定义Redis的键
                String redisKey = "SelectList:";
                try {
                    // 从数据库获取当前所有数据
                    String condition="";
                    List<SelectType> selectTypes = selectTypeMapper.selectList(null); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, selectTypes);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "添加成功";
            } else {
                return "添加失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "添加失败";
        }
    }

    @Override
    public String resultAdd(DeviceInspectiontaskResult deviceInspectiontaskResult) {
        System.out.println("project:" + deviceInspectiontaskResult.toString());
        try {
            System.out.println("更新设备维修经验库");
            // 尝试将数据插入数据库


            if (deviceInspectiontaskResultMapper.insert(deviceInspectiontaskResult) > 0) {
                // 定义Redis的键
                String redisKey = "ResultList:";
                try {
                    // 从数据库获取当前所有数据
                    String condition="";
                    List<DeviceInspectiontaskResult> deviceInspectiontaskResults = deviceInspectiontaskResultMapper.selectList(null); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, deviceInspectiontaskResults);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "添加成功";
            } else {
                return "添加失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "添加失败";
        }
    }

    @Override
    public String update(DeviceInspectionproject deviceInspectionproject) {
        System.out.println("project:" + deviceInspectionproject.toString());
        try {
            System.out.println("更新设备维修经验库");
            // 尝试将数据插入数据库

            UpdateWrapper uw=new UpdateWrapper();
            uw.set("InspectionProject_name",deviceInspectionproject.getInspectionprojectName());
            uw.set("InspectionProject_type_id",deviceInspectionproject.getInspectionprojectTypeId());
            uw.set("InspectionProject_state",deviceInspectionproject.getInspectionprojectState());
            uw.set("InspectionProject_content",deviceInspectionproject.getInspectionprojectContent());
            uw.set("notse",deviceInspectionproject.getNotse());
            uw.eq("code",deviceInspectionproject.getCode());
            if (deviceInspectionprojectMapper.update(deviceInspectionproject,uw) > 0) {
                // 定义Redis的键
                String redisKey = "ProjectList:";
                try {
                    // 从数据库获取当前所有数据
                    String condition="";
                    List<DeviceInspectionproject> deviceInspectionprojectList = deviceInspectionprojectMapper.select(null); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, deviceInspectionprojectList);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "修改成功";
            } else {
                return "修改失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "修改失败";
        }
    }


}




